const express = require('express')
//引入连接池
const r = express.Router();
const pool=require('../pool.js')
//console.log(pool)
//1.用户注册(post /reg)
//接口地址：http://127.0.0.1:8080/v1/users/reg
//请求方式
r.post('/reg', (req, res,next) => {
	//res.send({code:200,msg:'注册成功'})
	var obj = req.body
	console.log(obj)
	if (!obj.uname) {
		res.send({code: 401,msg: 'uname不能为空'})
	}else if (!obj.upwd) {
		res.send({code: 402,msg: 'upwd不能为空'})
	} else if (!obj.email) {
		res.send({code: 403,msg: 'email不能为空'})
	} else if (!obj.phone) {
		res.send({code: 405,msg: 'phone不能为空'})
	} else if(/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(obj.phone)){
		res.send({code: 405,msg: '手机号错误'})
	}else {
		pool.query('insert into xz_user set ?', [req.body], (err, result) => {
			if (err){
				//把错误抛给下一个中间件处理
				next(err);
				return;
			} 
			console.log(result);
			res.send({code: 200,msg: '员工添加成功'})
		})
	}
});
//2.用户登录
r.post('/login',(req,res,next)=>{
	var obj=req.body
	console.log(obj)
	if(!obj.uname){
		res.send({code:401,msg:'uname不能为空'})
		return
	} if(!obj.upwd){
		res.send({code:401,msg:'upwd不能为空'})
		return
	}
	pool.query('select *from xz_user where uname=? and upwd=? ',[obj.uname,obj.upwd],(err,result)=>{
		if (err){
			//把错误抛给下一个中间件处理
			next(err);
			return;
		} 
		console.log(result);
		//结果是数组，如果为空数组说明登录失败
		if(result.length!==0){
			res.send({code: 200,msg: '登录成功'})
		}else{
			res.send({code: 201,msg: '登录失败'})
		}
	})
})
//3.查找
r.get('/:uid',(req,res,next)=>{
	pool.query('select *from xz_user where uid= ?',[req.params.uid],(err,result)=>{
		if (err){
			//把错误抛给下一个中间件处理
			next(err);
			return;
		} 
		//console.log(result);
		if(result.length!==0){
			res.send({code:200,msg:'查找成功',data:result})
			}else{
				res.send({code:201,msg:'查找失败',data:'查无此人'})
			}
			
	});
})
/* r.post('/update',(req,res)=>{
	console.log(req.body);
	var $uid = req.body.uid;
	if(!$uid){
		res.send({code: 401,msg: 'uid required'});
		return;
	}
	var $uname = req.body.uname;
	if(!$uname){
		res.send({code: 402,msg: 'uname required'});
		return;
	}
	var $upwd = req.body.upwd;
	if(!$upwd){
		res.send({code: 403,msg: 'upwd required'});
		return;
	}
	var $email = req.body.email;
	if(!$email){
		res.send({code: 404,msg: 'email required'});
		return;
	}
	var $phone = req.body.phone;
	if(!$phone){
		res.send({code: 405,msg: 'phone required'});
		return;
	}
	var sql = `update xz_user set uname=?,upwd=?,email=?,phone=? where uid=?`;
	pool.query(sql,[$uname,$upwd,$email,$phone,$uid],(err,result)=>{
		if(err){
			throw error;
		}
		res.send('修改成功');
	});
}); */
r.post('/update',(req,res)=>{
  //4.1获取post请求的数据
  let obj=req.body;
  //console.log(obj);
  //4.2验证数据是否为空
  //遍历对象，访问每个属性
  let i=400;//记录状态码
  for(let key in obj){
	i++;//每遍历一个属性，值自动加1
	//key 属性名  obj[key]  属性值 
    //console.log(key,obj[key]);
    //如果属性值为空，提示该属性名必须提供
	if(!obj[key]){
	  res.send({code:i,msg:key+' required'});
	  return;
	}else{
		var sql = `update xz_user set uname=?,upwd=?,email=?,phone=? where uid=?`;
		pool.query(sql,[$uname,$upwd,$email,$phone,$uid],(err,result)=>{
			if(err){
				throw error;
			}
			res.send('修改成功');
	})
  }}
});
//导出路由器对象
module.exports = r
